<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_login
    //
	class t_field_login extends t_base_field
	{
        
        // property declaration
		// protected

        

        // property declaration
		// public
		// A
		const auto_login_type_none = 0;
		const auto_login_type_actived = 1;
		const auto_login_type_delayed = 2;
		const auto_login_type_last = 3;
		//B
		public $button_field = null;
		// D
		public $disconnect_field = null;
		// C
		public $create_field = null;
		// G
		const language_position_per_user = 1;
		const language_position_per_password = 2;
		const language_position_per_login = 3;
		const language_position_per_can_not_be_empty = 4;
		const language_position_per_invalid_value = 5;	
		const language_position_per_verification_correct_result = 6;
		const language_position_per_verification_error_result = 7;	
		const language_position_per_create = 8;		
		const language_position_per_disconnect = 9;
		const language_position_last = 10;		
		
		// E
		public $email_field = null;
		// P
		public $password_field = null;
		
		// P
		const proper_properties_position_per_verification_query = 1;
		const proper_properties_position_per_verification_query_parameters_names = 2; 
		const proper_properties_position_per_verification_query_result_file_name = 3; // does not include extension
		const proper_properties_position_per_insertion_query = 4;
		const proper_properties_position_per_insertion_query_parameters_names = 5; 
		const proper_properties_position_per_autologin = 6;
		const proper_properties_position_per_use_custom_language = 7;
        const proper_properties_position_last = 8;

        // method declaration
    	// protected
		// C
		protected function complete_field_tags($a_button_text,$a_label_text)
        {			
		

		}
		// G
        protected function get_action_name($a_prefix)
        {
            return $a_prefix . "_" . $this->name;
        }
		
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			$local_return = '';
			
			switch ($a_event_type) 
            {
                case t_base_field::field_event_type_on_selected:
					//echo "this->default_start_tag->name:". $this->default_start_tag->name;
					$this->default_start_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					$this->disconnect_field->main_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					
					
				break;
				case t_base_field::field_event_type_on_insert:
					if ($this->is_insertion_enabled())
						$this->create_field->main_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					
				break;                
			}
			return $local_return;
		}

		protected function is_CE_for_the_new_page($a_CE)
		{
			global $g_cuteengine;
			//print_r($a_CE);
			$local_component = $a_CE->page_component;
			//print_r($local_component);
			$local_return = (($this->has_login_request($local_component)) /*&& ($this->is_component_to_render($local_component))*/);
			$local_parent_id = $local_component->parent_id;
			while (($local_return) && ($local_parent_id != null) && ($local_parent_id != 0))
			{
				//echo "local_parent_id: $local_parent_id<br>";
				$local_component = $g_cuteengine->components[$local_parent_id][t_cuteengine::components_cuteengine_component]->page_component;
				$local_return = !$this->has_login_request($local_component);
				$local_parent_id = $local_component->parent_id;
			}
			//echo "local_return: $local_return";
			return $local_return;
		}

		protected function get_js_code_to_new_page($a_logged)
		{
			global $g_cuteengine;
			$local_result = '';
			foreach ($g_cuteengine->components as $local_component_id => $local_component) 
			{
                //echo "local_component_id: $local_component_id<br>";
				if ($local_component_id != '')
				{
					$local_site_component = $local_component[t_cuteengine::components_cuteengine_component];
					if ($this->is_CE_for_the_new_page($local_site_component ))
					{
						$local_page_component = $local_site_component->page_component;
						if ((! $a_logged) || ((! $this->is_component_to_render($local_page_component))))
							$local_result .= 'l_script = "";';
						else
						{
							$local_search[] =  '"'; 
							$local_replace[] = '\"';
							$local_result .= 'l_script = "'.
								//str_replace($local_search, $local_replace,$local_page_component->generate_html_code(/*$g_cuteengine->display_processed_component($local_component_id)*/)).'";';
								str_replace($local_search, $local_replace,$g_cuteengine->display_processed_component($local_component_id)).'";';
							
						}		
						$local_result .= 'set_tag_property([\'innerHTML\'],[l_script],window.cuteengine.html_tag("'.$local_page_component->to_logging_tag->name.'"),true);';	
								
					}
				}	
            }
			return $local_result;
		}

static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        }
	
		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str)
		{
            global $g_cuteengine;
			global $g_ambient_namespace;
			parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            
			$local_fields = "|t_field_email|".$this->name."_email|-1|0|".$this->lang[self::language_position_per_user]."|";
			$local_fields .= "|t_field_text|".$this->name."_password|-1|0|".$this->lang[self::language_position_per_password]."|";
			$local_fields .= "|t_field_button|".$this->name."_login|-3|0|".$this->lang[self::language_position_per_login];
			$local_fields .= "|t_field_button|".$this->name."_disconnect|-3|0|".$this->lang[self::language_position_per_disconnect]."|";
			if ($this->is_insertion_enabled())
			{
				$local_fields .= "t_field_button|".$this->name."_create|-3|0|".$this->lang[self::language_position_per_create]."|";
			}
			$this->parent_part->add_field_uses_as_included($local_fields);
			// LANGUAGES
            if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_use_custom_language]) == 'TRUE')
                $g_ambient_namespace->load_custom_language($this,$this->name);
            // verification
            //$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_per_verification_query_parameters_names],1,-1)).'"]';
			$local_params = $this->proper_properties_array[self::proper_properties_position_per_verification_query_parameters_names];
            $this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('verification').'",["'.$this->name.'"],"'.
				$this->proper_properties_array[self::proper_properties_position_per_verification_query].'",'.$local_params.',"FALSE","TRUE"]');
            // insertion
			if ($this->is_insertion_enabled())
			{
				$local_params = $this->proper_properties_array[self::proper_properties_position_per_insertion_query_parameters_names];
				$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('insertion').'",["'.$this->name.'"],"'.
					$this->proper_properties_array[self::proper_properties_position_per_insertion_query].'",'.$local_params.',"FALSE","TRUE"]');
			}
            if (! isset($g_cuteengine->login_field))
				$g_cuteengine->login_field = $this;
			
		}
		// A
		public function apply_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments)
        {
			global $g_ambient_namespace;
			$local_action = $a_arguments[0];
            $local_action_name = $local_action->get_action_name();
			//echo "local_action_name: $local_action_name<br>";
            $local_result = $a_arguments[1];
           // print_r($local_result);
            
			$local_file_name = trim($this->proper_properties_array[self::proper_properties_position_per_verification_query_result_file_name]);
				//echo "local_file_name: $local_file_name<br>";
			if ($local_file_name != '')
			{
                $local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
                require($local_file_name);
			}
			else
			{
				if (($local_action_name == $this->get_action_name('insertion')))
				{

				
					$local_result->add_function_result(
                        "","","","local_tag = window.cuteengine.html_tag('".$this->default_start_tag->name."');local_tag.click();");        

				}
				else
				{
					//echo "a_rows_count = $a_rows_count - a_query_parameter->sql_values[0]: ".$a_query_parameter->sql_values[0]."<br>";
					 
					$local_js_result = '';
					$local_log_result = true;
					$local_logged_user = $g_ambient_namespace->is_there_a_logged_user();
					if (!$g_ambient_namespace->is_there_a_logged_user())
					{
						if ($a_query_parameter->sql_values[0] > -1) 
						{
							global $g_ambient_namespace;
							$g_ambient_namespace->set_user_info($a_query_parameter->sql_values["user_name"],$a_query_parameter->sql_values["user_groups_list"],$a_query_parameter->sql_values["user_email"],
								$a_query_parameter->sql_values["user_id"]);
							$local_js_result = $this->get_js_code_to_new_page(true);
							//echo "local_js_result: $local_js_result<br>";
							$local_text = $this->lang[self::language_position_per_verification_correct_result];
							$local_log_result = true;
							
						}        
						else
						{
							$local_js_result = $this->get_js_code_to_new_page(false);
							$local_text = $this->lang[self::language_position_per_verification_error_result];
							$local_log_result = false; 
						}        
					}	
					else
					{
					//	echo "FREE USER<br>";
						$g_ambient_namespace->free_user_info();
						$local_js_result = $this->get_js_code_to_new_page(false);
						$local_text = $this->lang[self::language_position_per_verification_correct_result];
					}
                    $local_search[] =  '"'; 
					$local_replace[] = '\"';
					
					if (($local_log_result == true) && (!$local_logged_user))
					{
						
						$local_result->add_function_result("","","","location.reload();");  
					}
					else
					{
						echo 
						$this->set_text_content();
						if (($local_logged_user) && (!$g_ambient_namespace->is_there_a_logged_user()))
						{
							$log_message = "";
						}
						else
							$log_message = "window.cuteengine.html_tag(window.cuteengine.".$this->name.".log_componet_name).innerHTML = '$local_text';"; 
						$local_result->add_function_result(
							"","","",$local_js_result.
							"window.cuteengine.html_tag('".$this->main_tag->name."').innerHTML = \"".str_replace($local_search, $local_replace,$this->main_tag->generate_inner_html_code())."\";".$log_message);
					}
					   
				}
			}
			
		}
        // C
		public function create_tags_structure()
        {
			
            //echo __METHOD__."<BR>";
            global $g_javascript_namespace;
			parent::create_tags_structure();
			// method

			// component
            $local_name = $this->common_properties_array[t_base_field::common_properties_position_per_field_name];
            $local_current_tag = parent::add_tag(t_base_tag_type::btt_form,
                $local_name);
			$local_current_tag->add_property('onsubmit','return false');
			$this->main_tag = $local_current_tag;
			$this->default_start_tag = $this->main_tag;
            $this->default_result_tag = $this->main_tag;
			
            // label style
            $this->add_style_sheet_property("#".$this->main_tag->name."","width","200px");
            $this->add_style_sheet_property("#".$this->main_tag->name." label","display","block");
            $this->add_style_sheet_property("#".$this->main_tag->name." label","width","100%");
            // input, textarea style
            $this->add_style_sheet_property("#".$this->main_tag->name." input,#".$this->main_tag->name." textarea","display","block");
            $this->add_style_sheet_property("#".$this->main_tag->name." input,#".$this->main_tag->name." textarea","width","70%");
            $this->add_style_sheet_property("#".$this->main_tag->name." input,#".$this->main_tag->name." textarea","margin-right","10px");
            $this->add_style_sheet_property("#".$this->main_tag->name." input,#".$this->main_tag->name." textarea","margin-left","10px");
            $this->add_style_sheet_property("#".$this->main_tag->name." input,#".$this->main_tag->name." textarea","margin-bottom","10px");

            // button style
            $this->add_style_sheet_property("#".$this->main_tag->name." button","display","block");
            $this->add_style_sheet_property("#".$this->main_tag->name." button","padding","3px");
			
			//
			
            
            
        }
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
            global $g_strings_utility_namespace;
			// Methods    
			// P

		}
		// E
		public function execute_verification_action($a_ignore_result = false)
        {
			global $g_cuteengine;
			$local_result = new t_content_loader_result;
			$local_action = $g_cuteengine->actions_list[md5($this->get_action_name('verification'))];
			if ($a_ignore_result)
				ob_start();
			$local_action->do_action_from_ajax($local_result);
			if ($a_ignore_result)
				ob_end_clean() ;
			/*if (! $local_result->succeeded)
			{
				echo "ERROR, action verification eseguita con errore!";
			}
			else
			{
				echo "OK, action verification eseguita con SUCCESSO!";
			}*/
		}
		
		public function set_text_content()
        {
			global $g_ambient_namespace;
			$local_create_field = '';
			if ($this->is_insertion_enabled())
				$local_create_field = $this->create_field->get_included_in_html_structure($this->main_tag->name."_create",!$g_ambient_namespace->is_there_a_logged_user(),'');
			$local_content = $this->email_field->get_included_in_html_structure($this->main_tag->name."_email",!$g_ambient_namespace->is_there_a_logged_user(),'').
				$this->password_field->get_included_in_html_structure($this->main_tag->name."_password",!$g_ambient_namespace->is_there_a_logged_user(),'').
				$this->button_field->get_included_in_html_structure($this->main_tag->name."_login",!$g_ambient_namespace->is_there_a_logged_user(),'').
				$this->disconnect_field->get_included_in_html_structure($this->main_tag->name."_disconnect",$g_ambient_namespace->is_there_a_logged_user(),'').
				$local_create_field;
			//echo "local_content: $local_content<br>";
			$this->main_tag->set_text_to_include($local_content);
		}	
		// F
		public function finalize_creation_field()
        {
			
            global $g_cuteengine;
			global $g_ambient_namespace;
			parent::finalize_creation_field();
			$this->email_field = $this->parent_part->get_field($this->main_tag->name."_email");
			$this->password_field = $this->parent_part->get_field($this->main_tag->name."_password");
			$this->password_field->main_tag->add_property("type","password");	
			$this->button_field = $this->parent_part->get_field($this->main_tag->name."_login");
			$this->disconnect_field = $this->parent_part->get_field($this->main_tag->name."_disconnect");
			if ($this->is_insertion_enabled())
			{
				$this->create_field = $this->parent_part->get_field($this->main_tag->name."_create");
			}
			$this->default_start_tag = $this->button_field->main_tag;
			$this->get_field_event_script(t_base_field::field_event_type_on_selected,"event");
			$this->get_field_event_script(t_base_field::field_event_type_on_insert,"event");
			$this->set_text_content();
			
			if ($this->is_autologin_actived())
				$this->execute_verification_action(true);
			else
			{
				//$g_ambient_namespace->free_user_info();
				//$this->execute_verification_action(true);
			}
			// Execute on autologin
			/*switch ($this->proper_properties_array[self::proper_properties_position_per_autologin]) 
            {
                case self::auto_login_type_none:
				break;
                case self::auto_login_type_actived:
				break;
                case self::auto_login_type_delayed:
				break;                
			}*/
				//echo "finalize_creation_field<br>";
				
			
		}
				
		// G
        public function get_event_name($a_event_type)
        {

            return /*get_class($this)*/$this->name . "_event_".$a_event_type;
        }
        public function get_event_type($a_ction_name)
        {	
			if ($a_ction_name == $this->get_action_name('verification'))
				return t_base_field::field_event_type_on_selected;
			else
				return t_base_field::field_event_type_on_insert;			
		}	
		
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }
		// I
		public function initialize_field_event($a_event_type)
        {			
			global $g_javascript_namespace;
            global $g_strings_utility_namespace;
            global $g_url_parameters_namespace;
            //$local_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->parent_component->id);
			//print_r($local_parameter);
			$local_event = $this->events[$a_event_type];
		//	echo "local_event->event_name: ".$local_event->event_name." - local_event->event_type: $local_event->event_type<br>";
			switch ($local_event->event_type) 
            {
                /***********  t_base_field::field_event_type_on_selected  ***********/                
				case t_base_field::field_event_type_on_selected:
				case t_base_field::field_event_type_on_insert:
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
						get_class($this),$local_event->event_name,'event',"
       var local_argoument = '"./*t_url_parameter::separator_level_0 . "' + '".
	   str_replace(t_url_parameter::separator_level_0, t_url_parameter::separator_level_1,$local_parameter->argument).*/"';
       ".$local_event->body_code."
       ");
	   
				break;

			}	
            
		}
		public function is_autologin_actived()
		{		
			//echo "is_autologin_actived:".$this->proper_properties_array[self::proper_properties_position_per_autologin]."<br>";
			return ($this->proper_properties_array[self::proper_properties_position_per_autologin] == self::auto_login_type_actived);		
		}	

		public function is_insertion_enabled()
		{
			return (trim($this->proper_properties_array[self::proper_properties_position_per_insertion_query]) != '');
		}
		
		public function has_login_request($a_component)
		{		
			$local_permission = $a_component->get_to_login_permission();
			return ((trim($local_permission) != '') && 
				($local_permission != NULL));
			
		}
		
		public function is_component_to_render($a_component)
		{		
			global $g_ambient_namespace;
			$local_permission = $a_component->get_to_login_permission();
			return ((! $this->has_login_request($a_component)) or
				($g_ambient_namespace->check_user_permission($local_permission)));
			//return ($this->proper_properties_array[self::proper_properties_position_per_autologin] == self::auto_login_type_actived);		
		}
		
    }
?>    